﻿using AntdUI;

namespace TestPressure
{
    public partial class TemplateManageForm : BaseForm
    {

        #region

        Template? obj;

        #endregion

        #region 自定义方法

        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<Template> list = SqlSugarHelper.Inquireable<Template>(null);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<Template> list)
        {
            var antItemList = new List<TemplateItem>();
            foreach (Template item in list)
            {
                string first = string.Empty;
                if (item.first_stage_pressure > 0 && item.first_stage_minute> 0)
                {
                    first = string.Format("压力:{0}Mpa--时长：{1}h", item.first_stage_pressure.ToString(), item.first_stage_minute.ToString());
                }

                string second = string.Empty;
                if (item.second_stage_pressure > 0 && item.second_stage_minute > 0)
                {
                    second = string.Format("压力:{0}Mpa--时长：{1}h", item.second_stage_pressure.ToString(), item.second_stage_minute.ToString());
                }
                
                string third = string.Empty;
                if (item.third_stage_pressure > 0 && item.third_stage_minute > 0)
                {
                    third = string.Format("压力:{0}Mpa--时长：{1}h", item.third_stage_pressure.ToString(), item.third_stage_minute.ToString());
                }
                
                string rigor = string.Empty;
                if (item.rigor_stage_pressure > 0 && item.rigor_stage_minute > 0)
                {
                    rigor = string.Format("压力:{0}Mpa--时长：{1}h", item.rigor_stage_pressure.ToString(), item.rigor_stage_minute.ToString());
                }
                
                string intensity = string.Empty;
                if (item.intensity_stage_pressure > 0 && item.intensity_stage_minute > 0)
                {
                    intensity = string.Format("压力:{0}Mpa--时长：{1}h", item.intensity_stage_pressure.ToString(), item.intensity_stage_minute.ToString());
                }

                string pipe_position = string.Empty;
                if (item.pipe_position is not null)
                {
                    pipe_position = item.pipe_position;
                }

                string edit = "否";
                if (item.edit_enable == 1)
                {
                    edit = "是";
                }
                antItemList.Add(new TemplateItem(item.template_number, pipe_position, item.design_pressure,
                    first, second, third, intensity, rigor, edit));
            }
            Table.DataSource = antItemList;
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckRowExist(int number)
        {
            obj = ObjectWithPrimaryKey(number);
            if (obj == null)
            {
                LoadList();
                string msg = String.Format("编号【{0}】的模板不存在", number);
                Error(msg,this);
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>根据主键查询记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Template? ObjectWithPrimaryKey(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["template_number"] = number;
            List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region 系统方法
        public TemplateManageForm()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterParent;
            FullScreen = true;
        }

        private void TemplateManageForm_Load(object sender, EventArgs e)
        {
            Header.Text = "模板管理";
            Table.Height = Height - Header.Height - StackPanel1.Height;
            Table.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("template_number","模板编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("pipe_position","管道位置",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("design_pressure","设计压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("first_stage","第一阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("second_stage","第二阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("third_stage","第三阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("intensity_stage","强度阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("rigor_stage","严密阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("operation","操作",AntdUI.ColumnAlign.Center)
            };
            Spin.Config config = new Spin.Config
            {
                Text = "正在加载中...",
            };
            Table.Spin(config, () =>
            {
                LoadList();
            }, () =>
            {

            });
        }
        #endregion

        #region 响应事件
        private void AddButton_Click(object sender, EventArgs e)
        {
            TemplateAddForm frm = new TemplateAddForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        /// <summary>点击表格操作按钮
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Table_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            if (e.Record is TemplateItem data)
            {
                CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                }
                else if (link.Id == "delete")
                {
                    bool res = CheckRowExist(data.template_number);
                    if (!res)
                    {
                        return;
                    }

                    string msg = string.Format("您确定要删除【{0}】模板吗？", data.template_number);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            if (obj is not null)
                            {
                                List<Template> list = new List<Template>();
                                list.Add(obj);
                                int deleteRes = SqlSugarHelper.Deleteable(list);
                                if (deleteRes > 0)
                                {
                                    LoadList();
                                }
                                else
                                {
                                    Error("删除模板失败",this);
                                }
                            }
                            else
                            {
                                Error("删除模板失败", this);
                            }
                            return true;
                        },
                    });
                }
            }
        }
        #endregion
    }
}
